<?php

namespace app\common\model\user;
use app\common\model\BaseModel;
use app\common\model\lawyer\Lawyer;
use app\common\model\system\Category;
use app\common\model\system\SystemCity;
use app\common\repositories\system\CategoryRepository;
use app\common\repositories\system\SystemCityRepository;
use app\common\repositories\user\UserExtensionRecordRepository;
use app\common\repositories\user\UserMemberRepository;
use app\common\repositories\user\UserOperationRepository;
use think\db\BaseQuery;

class User extends BaseModel
{

    public static function tablePK(): ?string
    {
        return 'uid';
    }


    public static function tableName(): string
    {
        return 'user';
    }


    public function getLawyerIdAttr()
    {
        $lawyer = Lawyer::getDB()->where('uid',$this->uid)->where('status',1)->find();
        return $lawyer ? $lawyer->lawyer_id : null;
    }


    public function getLawyerAttr()
    {
        $lawyer = Lawyer::getDB()->where('uid',$this->uid)->where('status',1)->find();
        return $lawyer ?: null;
    }



    public function getAvatarAttr($value)
    {
        return $value ?: 'http://116.62.214.87:8787/default_avatar.png';
    }


    public function getIdentityAttr()
    {
        $key = 'union_identity_';
        $cate = Category::getDB()->where('type','like',"%{$key}%")
            ->where('category_id',$this->identity_id)
            ->where('is_show',1)
            ->find();

        return $cate ? app()->make(CategoryRepository::class)->getCompleteName($cate->category_id,',') : '';

    }


    public function getSpreadUserAttr()
    {
        return User::getDB()->where(['status'=>1,'uid'=>$this->spread_uid])->find();
    }


    public function getTopUserAttr()
    {
        $spreadUser = $this->SpreadUser;
        if(!$spreadUser) return null;
        return User::getDB()->where(['status'=>1,'uid'=>$spreadUser->spread_uid])->find();
    }

    public function getFocusStatus($uid)
    {
        return app()->make(UserOperationRepository::class)->getStatus('focus_user',$this->uid,$uid);
    }


    public function getFirstSpreadRateAttr()
    {
        $map = [
            '0'=>'province',
            '1'=>'city',
           // '2'=>'district'
        ];

        $leader = $this->leader;
        $type = $leader && isset($map[$leader->level]) ? $map[$leader->level] : 'user';

        $rate = systemConfig('distribution_'.$type."_first_rate");
        if(!$rate || $rate < 0) return 0;
        if($rate > 50) return 50;
        return $rate;
    }

    public function getSecondSpreadRateAttr()
    {
        $map = [
            '0'=>'province',
            '1'=>'city',
            // '2'=>'district'
        ];

        $leader = $this->leader;
        $type = $leader && isset($map[$leader->level]) ? $map[$leader->level] : 'user';

        $rate = systemConfig('distribution_'.$type."_second_rate");
        if(!$rate || $rate < 0) return 0;
        if($rate > 50) return 50;
        return $rate;
    }


    public function leader()
    {
        return $this->hasOne(UserLeader::class,'uid','uid');
    }

    public function getGroupUserCountAttr()
    {
        $level1Uid = User::getDB()->where('spread_uid',$this->uid)->column('uid');
        $level2Count = User::getDB()->whereIn('spread_uid',$level1Uid)->count('uid');
        return count($level1Uid) + $level2Count;
    }

    public function getBusinessAttr()
    {
        $cate = Category::getDB()->where('type','union_business')
            ->where('category_id',$this->business_id)
            ->where('is_show',1)
            ->find();

        return $cate ? app()->make(CategoryRepository::class)->getCompleteName($cate->category_id,',') : '';
    }

    public function getCityAttr()
    {
        return app()->make(SystemCityRepository::class)->getCompleteAddress($this->city_id,' ');
    }

    public function getTagAttr($value)
    {
        return $value ? explode(',',$value) : [];
    }


    public function getGroupUserIdsAttr()
    {
        $level1Uid = User::getDB()->where('spread_uid',$this->uid)->column('uid');
        $level2Uid = User::getDB()->whereIn('spread_uid',$level1Uid)->column('uid');
        return array_merge($level1Uid,$level2Uid);
    }


    public function getMemberNameAttr()
    {
       // return '普通会员';

        /**@var $make UserMemberRepository**/
        $make = app()->make(UserMemberRepository::class);
        $member = $make->memberSearch($this->uid, ['type'=>1, 'status'=>1 , 'in_time'=>true,/*'is_default'=>0*/])->order('rank DESC')->find();
        return $member ? $member->member_name : '';
    }

    public function getExtUrlAttr($value)
    {
        if($value) return $value;
        return app()->make(UserExtensionRecordRepository::class)->getUserExtCard($this->uid);
    }


    public function searchBusinessIdAttr(BaseQuery $query,$value)
    {
        $ids = [$value];
        $cate = app()->make(CategoryRepository::class)->get($value);
        if($cate) $ids = array_merge($cate->ChildIds,$ids);
        $query->whereIn('business_id',$ids);
    }

    public function searchIdentityIdAttr(BaseQuery $query,$value)
    {
        $ids = [$value];
        $cate = app()->make(CategoryRepository::class)->get($value);
        if($cate) $ids = array_merge($cate->ChildIds,$ids);
        $query->whereIn('identity_id',$ids);
    }

    public function searchCityIdAttr(BaseQuery $query,$value)
    {
        $ids = [$value];
        $cityIds = SystemCity::getDB()->where('parent_id',$value)->column('city_id');
        $xianIds = SystemCity::getDB()->whereIn('parent_id',$cityIds)->column('city_id');
        $ids = array_merge($ids,$cityIds,$xianIds);
        $query->whereIn('city_id',$ids);
    }
}